﻿Public Class frmThemHocSinh
    Private IsAdd As Boolean = True
    Private HS As DTOHocSinh = New DTOHocSinh
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Public Sub New(ByVal hs As DTOHocSinh)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.IsAdd = False
        Me.HS = hs
    End Sub
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim hs As DTOHocSinh = New DTOHocSinh
        hs.TenHS = txtTen.Text
        hs.DiaChi = txtDiaChi.Text
        hs.Email = txtEmail.Text
        hs.NgaySinh = dtpNgaySinh.Value
        hs.GioiTinh = cbbGioiTinh.SelectedText
        hs.TenLop = cbbTenLop.SelectedValue
        hs.MaHS = Me.HS.MaHS
        If IsAdd = True Then
            Dim insert As DAOHocSinh = New DAOHocSinh
            If insert.Insert(hs, Application.StartupPath + "\database\database.mdb") Then
                Dim frm As frmHocSinh = New frmHocSinh
                frm.Show()
                Me.Close()
            Else
                MessageBox.Show("Thêm không thành công!", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                txtTen.Focus()
                Return
            End If
        Else
            Dim update As DAOHocSinh = New DAOHocSinh

            If update.Update(hs, Application.StartupPath + "\database\database.mdb") Then
                Dim frm As frmHocSinh = New frmHocSinh
                frm.Show()
                Me.Close()
            Else
                MessageBox.Show("Sửa không thành công!", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                txtTen.Focus()
                Return
            End If

        End If

    End Sub
    Public Sub LoadLop()
        Dim da As DAOLop = New DAOLop
        Dim ds As DataTable = New DataTable
        ds = da.LoadAllData(Application.StartupPath + "\database\database.mdb").Tables("Lop")
        cbbTenLop.DataSource = ds
        cbbTenLop.DisplayMember = "TenLop"
        cbbTenLop.ValueMember = "TenLop"
    End Sub
    Public Sub LoadData()
        If IsAdd = False Then
            txtTen.Text = HS.TenHS
            txtDiaChi.Text = HS.DiaChi
            txtEmail.Text = HS.Email
            dtpNgaySinh.Value = Convert.ToDateTime(HS.NgaySinh)

            If HS.GioiTinh = "Nam" Then
                cbbGioiTinh.SelectedIndex = 0
            Else
                cbbGioiTinh.SelectedIndex = 1
            End If

            Dim _hs As DAOHocSinh = New DAOHocSinh
            Dim hocsinh As DataTable = New DataTable
            hocsinh = _hs.LoadMa(HS.MaHS, Application.StartupPath + "\database\database.mdb")
            cbbTenLop.DataSource = hocsinh
            cbbTenLop.DisplayMember = "TenLop"
            cbbTenLop.ValueMember = "TenLop"
        Else
            LoadLop()
            cbbGioiTinh.SelectedIndex = 0
        End If
    End Sub
    Private Sub frmThemHocSinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Dim frm As frmHocSinh = New frmHocSinh
        frm.Show()
        Me.Close()
    End Sub

    Private Sub cbbTenLop_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbTenLop.TextChanged
        LoadLop()
    End Sub
End Class